package com.koushikdutta.async.future;

public class SimpleCancellable implements DependentCancellable {
    static final /* synthetic */ boolean $assertionsDisabled;
    public static final Cancellable COMPLETED = new SimpleCancellable() {
        {
            setComplete();
        }

        public /* bridge */ /* synthetic */ DependentCancellable setParent(Cancellable x0) {
            return super.setParent(x0);
        }
    };
    boolean cancelled;
    boolean complete;
    private Cancellable parent;

    static {
        boolean z;
        if (SimpleCancellable.class.desiredAssertionStatus()) {
            z = false;
        } else {
            z = true;
        }
        $assertionsDisabled = z;
    }

    public boolean isDone() {
        return this.complete;
    }

    protected void cancelCleanup() {
    }

    protected void cleanup() {
    }

    protected void completeCleanup() {
    }

    public boolean setComplete() {
        boolean z = true;
        synchronized (this) {
            if (this.cancelled) {
                z = false;
            } else if (!this.complete) {
                this.complete = true;
                this.parent = null;
                completeCleanup();
                cleanup();
            } else if ($assertionsDisabled) {
            } else {
                throw new AssertionError();
            }
        }
        return z;
    }

    public boolean cancel() {
        boolean z = true;
        synchronized (this) {
            if (this.complete) {
                z = false;
            } else if (this.cancelled) {
            } else {
                this.cancelled = true;
                Cancellable parent = this.parent;
                this.parent = null;
                if (parent != null) {
                    parent.cancel();
                }
                cancelCleanup();
                cleanup();
            }
        }
        return z;
    }

    public SimpleCancellable setParent(Cancellable parent) {
        synchronized (this) {
            if (!isDone()) {
                this.parent = parent;
            }
        }
        return this;
    }

    public boolean isCancelled() {
        boolean z;
        synchronized (this) {
            z = this.cancelled || (this.parent != null && this.parent.isCancelled());
        }
        return z;
    }

    public Cancellable reset() {
        cancel();
        this.complete = false;
        this.cancelled = false;
        return this;
    }
}
